package cn.lanqiao.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import cn.lanqiao.entity.Score;

import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TeacherAddSourceFrm extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField textid;
	private JTextField textnum;
	private JTextField textsoure;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			TeacherAddSourceFrm dialog = new TeacherAddSourceFrm();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	 public TeacherAddSourceFrm(Score addUser) {
		 this();
	     this.textid.setText(addUser.getSid());
	     this.textnum.setText(addUser.getScourse());
	     this.textsoure.setText(addUser.getSgrade());

	 }
	 
	 public TeacherAddSourceFrm(Score addUser,JTable table) {
		 this();
		 this.textid.setText(addUser.getSid());
	     this.textnum.setText(addUser.getScourse());
	     this.textsoure.setText(addUser.getSgrade());
	     this.table=table;
	     
	 }

	/**
	 * Create the dialog.
	 */
	public TeacherAddSourceFrm() {
		setTitle("添加学生成绩");
		setBounds(100, 100, 623, 379);
		TeacherAddSourceFrm.this.setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JButton btnNewButton = new JButton("学生编号");
		btnNewButton.setBounds(35, 28, 113, 27);
		contentPanel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("课程编号");
		btnNewButton_1.setBounds(35, 98, 113, 27);
		contentPanel.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("学生成绩");
		btnNewButton_2.setBounds(35, 172, 113, 27);
		contentPanel.add(btnNewButton_2);
		
		textid = new JTextField();
		textid.setBounds(287, 29, 130, 26);
		contentPanel.add(textid);
		textid.setColumns(10);
		
		textnum = new JTextField();
		textnum.setBounds(287, 99, 130, 27);
		contentPanel.add(textnum);
		textnum.setColumns(10);
		
		textsoure = new JTextField();
		textsoure.setBounds(287, 184, 130, 27);
		contentPanel.add(textsoure);
		textsoure.setColumns(10);
		
		JButton btnNewButton_3 = new JButton("确认添加");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 添加学生信息
//				String uuid =UUID.randomUUID().toString();
				//创建一个新文本框;
				String sid=textid.getText();
				String scourse=textnum.getText();
				String sgrade=textsoure.getText();
				//封装成一个新的对象
				Score score=new Score(sid,scourse,sgrade);
				//调用业务层
				cn.lanqiao.service.ScoreService service=new cn.lanqiao.service.impl.ScoreServiceImpl();
				service.addScores(score);
				//关闭
				TeacherAddSourceFrm.this.dispose();
				Object[][] datas = service.getAllScore();
			    String[] strs = new String[] {"学号", "课程编号", "\u6210\u7EE9"};
			    table.setModel(new DefaultTableModel(datas,strs));
			}
		});

		btnNewButton_3.setBounds(53, 257, 130, 52);
		contentPanel.add(btnNewButton_3);
		
		JButton btnNewButton_4 = new JButton("取消");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TeacherAddSourceFrm.this.dispose();
			}
		});
		btnNewButton_4.setBounds(366, 257, 130, 52);
		contentPanel.add(btnNewButton_4);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
		}
	}
	
	
	public TeacherAddSourceFrm(JTable table) {
		this();
		this.table=table;
	}
	
	
}
